package com.runshu.newsmartark.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.runshu.newsmartark.base.MyApplication;
import com.runshu.newsmartark.common.ConstantUrl;
import com.runshu.newsmartark.dbutil.RefreshUtils;
import com.runshu.newsmartark.network.netbean.HttpRequestParamsBuilder;
import com.runshu.newsmartark.util.MacUtils;
import com.runshu.newsmartark.util.common.SharedPreferencesUtils;
import com.runshu.newsmartark.util.y.nativelog.NLogUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.runshu.newsmartark.util.StringUtils.sdf;


public class SyncDataService extends Service {
    int refreshAllDataTime =30 * 60 * 1000;

    SyncDataTask task;

    @Override
    public void onCreate() {
        super.onCreate();
        task = new SyncDataTask();
        task.start();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand( intent, flags, startId );
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        task.stop();
        task = null;
    }

    public class SyncDataTask extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
                while (true) {
                    String time = (String) SharedPreferencesUtils.get( MyApplication.getApplication(), ConstantUrl.Share_RefreshProTime, "" );

                    sleep( 1000 * 60 );
                    if (TextUtils.isEmpty( time )) {
                        refresh();
                    } else {
                        Date parse = sdf.parse( time );
                        if (System.currentTimeMillis() - parse.getTime() > refreshAllDataTime) {
                            refresh();
                        }
                    }
                    Log.w( "syncService", time );
                }
            } catch (InterruptedException | ParseException e) {
                e.printStackTrace();
            }
        }

        private void refresh() {
            SharedPreferencesUtils.put(MyApplication.getApplication(), ConstantUrl.Share_RefreshProTime, sdf.format(new Date()));

            Log.w( "syncService", "===========同步数据！！！" );
            //刷新用户信息
            RefreshUtils.getInstance().refreshUserinfo();
            //刷新所有产品信息
//            RefreshUtils.getInstance().refreshProAllInfo();
            //获取智能柜基本信息
//            RefreshUtils.getInstance().refreshCabinetInfo();
            //上载log信息
//            RefreshUtils.getInstance().insertHardwareLog( null );
            //监听是否在线
//            RefreshUtils.getInstance().HWHeart();
            //同步时间
//            NLogUtil.i( NLogUtil.INFO_Sync_Record_Time, (String) SharedPreferencesUtils.get( SyncDataService.this, ConstantUrl.Share_RefreshTime, "暂无" ) );
        }


    }
}
